// pages/folder/folder.js
import {
  request
} from '../../../../service/network.js';
import {
  getSetting,
  dataGroup
} from '../../../../utils/utils.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classify: '',
    edit: null,
    isNightMode: false,
    fileListData: {
      listConfig: {
        isFolder: false
      },
      listData: []
    },
    isLoginStatus: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let folderID = parseInt(options.classify)
    wx.setNavigationBarTitle({
      title: options.name
    })
    this.getFileData(folderID)
  },
  async getFileData(folderID) {
    let res = await getSetting()
    let userInfo = wx.getStorageSync('userInfo')
    if (res.authSetting['scope.userInfo'] && userInfo.id) {
      let fileData = wx.getStorageSync('fileData');
      if (fileData.userId && Date.now() - fileData.time < 1000 * 10) {
        this.setData({
          'fileListData.listData': fileData.data[folderID] ? fileData.data[folderID] : []
        })
      } else {
        this.sendRequest(userInfo.id, folderID)
      }
    }
  },
  async sendRequest(userId, folderID) {
    let res1 = await request({
      url: '/filedata/getfilelist.php',
      method: 'POST',
      data: {
        userid: userId
      }
    })
    if (res1.status == 200) {
      let newData = dataGroup(res1.data, 'folderid')
      wx.setStorageSync('fileData', {
        userId,
        time: Date.now(),
        data: newData
      });
      this.setData({
        'fileListData.listData': newData[folderID] ? newData[folderID] : []
      })
    }
  },
  onMyEvent: function (e) {
    this.setData({
      edit: e.detail.edit
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})